# -*- coding: utf-8 -*-
'''
Created on 2011-3-3

@author: Tony
'''
from django import forms
from django.contrib.auth.models import User,check_password
from django.utils.translation import ugettext_lazy as _
from goplay.sinaweibo.models import UserInfo,Province


GENDER_CHOICE=[['m','男'],['f','女']]

provinces = Province.objects.all().distinct().values('province_id','province_name')
PROVINCE_CHOICES = []
for province in provinces:
    PROVINCE_CHOICES.append([province.get("province_id"), province.get("province_name")])     

#CITY_CHOICES=[]


class RegisterForm(forms.Form):
    email=forms.EmailField(label=_(u"邮箱"),max_length=30,widget=forms.TextInput)
    nickname=forms.CharField(label=_(u"昵称"),max_length=30,widget=forms.TextInput)
    password=forms.CharField(label=_(u"密码"),max_length=30,widget=forms.PasswordInput)
    gender=forms.ChoiceField(label=_(u"性别"),choices=GENDER_CHOICE)
    province = forms.ChoiceField(widget=forms.Select(attrs={'class':'province_select','onChange':'getCityOptions(this.value)'}),choices=PROVINCE_CHOICES,label= u'所在地',initial='')
    city=forms.CharField(label='',max_length=5,widget=forms.HiddenInput)
    

    
    #def clean_username(self):
    #   "验证重复的昵称"
    #    users=User.objects.filter(username__iexact=self.cleaned_data['username'])
    #    if not users:
    #        return self.cleaned_data['username']
    #    raise forms.ValidationError(_(u"该昵称已经被占用"))
    def clean_email(self):
        "验证重复的email"
        email=User.objects.filter(email__iexact=self.cleaned_data["email"])
        if not email:
            return self.cleaned_data["email"]
        raise forms.ValidationError(_(u"该邮箱已经被占用"))
    def clean_nickname(self):
        nickname=User.objects.filter(first_name__iexact=self.cleaned_data['nickname'])
        if not nickname:
            return self.cleaned_data['nickname']
        raise forms.ValidationError(_(u'该昵称已经被占用'))
  

class LoginForm(forms.Form):
    email=forms.CharField(label=_(u"邮箱"),max_length=30,widget=forms.TextInput)
    password=forms.CharField(label=_(u"密码"),max_length=30,widget=forms.PasswordInput)


class UserAccountForm(forms.Form):
    def __init__(self,request,*args,**kwargs):
        super (UserAccountForm,self).__init__(*args,**kwargs)
        userinfo=request.session['userinfo']
        location=UserInfo.objects.filter(username=userinfo['username']).values('province','city')[0]
             
        citys=Province.objects.filter(province_id=location.get("province")).values('city_id','city_name')
        if citys:
            CITY_CHOICES=[]
            for city in citys:
                CITY_CHOICES.append([city.get('city_id'),city.get('city_name')])
        #self.fields['province'] = forms.ChoiceField(label='Username',max_length=100,initial=request.session['some_var'])  
        self.fields['province']= forms.ChoiceField(widget=forms.Select(attrs={'class':'province_select','onChange':'getCityOptions(this.value)'}),choices=PROVINCE_CHOICES,label= u'所在地',initial=location.get('province'))
        self.fields['city']= forms.ChoiceField(widget=forms.Select(attrs={'class':'city_select'}),choices=CITY_CHOICES,label= u'',initial=location.get('city'))

    
class ChangePasswordForm(forms.Form):
    initial_password=forms.CharField(label=_(u"旧密码"),max_length=30,widget=forms.PasswordInput)
    new_password=forms.CharField(label=_(u"新密码"),max_length=30,widget=forms.PasswordInput)
    repeat_password=forms.CharField(label=_(u"重复密码"),max_length=30,widget=forms.PasswordInput)
    
    
    def __init__(self,user,*args,**kwargs):
        self.user=user
        super(ChangePasswordForm,self).__init__(*args,**kwargs)
    
    def clean_initial_password(self):
        password=self.cleaned_data['initial_password']
        ur=User.objects.filter(username=self.user).values('password')[0]
        if not check_password(password,ur.get('password')):
            raise forms.ValidationError('密码错误')
    def clean_repeat_password(self):
        p1=self.cleaned_data['new_password']
        p2=self.cleaned_data['repeat_password']
        if not p1==p2:
            raise forms.ValidationError('两次输入的密码不一致')
        
        
        
class UsericonForm(forms.Form):
    image=forms.ImageField()
    
    

    
